/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;


import entity.Era;
import entity.Player;
import exception.ValueNotFoundException;
import java.util.Collection;
import java.util.LinkedList;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import session.manager.CheckBeanLocal;
import session.manager.PlayerManagerBeanLocal;
import session.manager.ResourceManagerBeanLocal;
import session.timed.BattleTimedBeanLocal;

/**
 *
 * @author antonio
 */
@Stateless
public class BattleBean implements BattleBeanLocal {
    
    public static final String RESOURCE = "Militia";
    
    @EJB
    PlayerManagerBeanLocal playerManager;
    
    @EJB
    ResourceManagerBeanLocal resourceManager;
    
    @EJB
    CheckBeanLocal checkBean;
    
    @EJB
    BattleTimedBeanLocal timedBean;

    @Override
    public Collection<String> findPlayers(String username) {
        Player player = playerManager.find(username);
        if(player == null) {
            throw new ValueNotFoundException("Il giocatore specificato non è presente nel database");
        }
        Era era = player.getEra();
        Collection<String> collection = new LinkedList<String>();
        for (Player p : era.getPlayerCollection()) {
            if(! p.getUsername().equals(username)) {
                collection.add(p.getUsername());
            }
        }
        return collection;
    }

    @Override
    public boolean moveBattle(String username1, int amount, String username2) {
        if(! checkBean.checkResourceAmount(username1, RESOURCE, amount)) {
            return false;
        }
        resourceManager.decrese(username1, RESOURCE, amount);
        timedBean.initTimer(username1, amount, username2);
        return true;
    }

}
